﻿using Oca35.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Oca35
{
    public partial class BoardView : Form
    {
        public Player[] Players { get; set; }
        private Panel[] panels = new Panel[42];
        private PictureBox[] picBoxes = new PictureBox[4];
        private Board board = new Board();

        public BoardView()
        {
            InitializeComponent();
        }

        private void BoardView_Load(object sender, EventArgs e)
        {
            board.Logger.write += Logger_write;
            board.Players = Players;
            board.StartGame();

            BuildBoard();
        }

        void Logger_write(object sender, string e)
        {
            rtbActivities.Text = e + "\n" + rtbActivities.Text;

        }

        private void btnSortear_Click(object sender, EventArgs e)
        {
            btnSortear.Enabled = false;
            var previousLoc = board.CurrentPlayer.Location;
            board.PlayMyTurn(board.CurrentPlayer);
            MovePlayer(board.CurrentPlayer, previousLoc);

            btnSortear.Enabled = board.NextTurn();
        }

        private void MovePlayer(Player player, Square previousLoc)
        {
            var tlp = (TableLayoutPanel)panels[player.Location.Index].Controls[0];
            var picBox = (PictureBox)picBoxes[player.Index];
            tlp.Controls.Add(picBox, player.Index % 2, player.Index / 2);
        }

        private void BuildBoard()
        {
            for (int i = 0; i < tlpTablero.RowCount; i++)
            {
                for (int j = 0; j < tlpTablero.ColumnCount; j++)
                {
                    var panel = new Panel();
                    panel.BorderStyle = BorderStyle.FixedSingle;

                    var tlp = new TableLayoutPanel();
                    //Aca escribo los colores del panel del tablero
                    tlp.ColumnCount = 2;
                    tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
                    tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
                    tlp.RowCount = 2;
                    tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
                    tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
                    tlp.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left;

                    tlp.BackgroundImageLayout = ImageLayout.Center; 
                    switch (i)
                    {
                        case 5:
                            if (j==6)
                            {
                              
                                tlp.BackgroundImage = Resources.SquareUp;
                            }
                            break;
                        case 0:
                            if (j == 6)
                            {
                                tlp.BackgroundImage = Resources.SquareLeft;
                            }
                            else if (j==0)
                            {
                                tlp.BackgroundImage = Resources.SquareDown;
                            }
                            break;
                        case 4:
                            if (j==0)
                            {
                                tlp.BackgroundImage = Resources.SquareRight;
                            }
                            else if (j==5)
                            {
                                tlp.BackgroundImage = Resources.SquareUp;
                            }
                            break;
                        case 1:
                            if (j==5)
                            {
                                tlp.BackgroundImage = Resources.SquareLeft;
                            }
                            else if (j==1)
                            {
                                tlp.BackgroundImage = Resources.SquareDown;
                            }
                            break;
                        case 3:
                            if (j==1)
                            {
                                tlp.BackgroundImage = Resources.SquareRight;
                            }
                            else if (j==4)
                            {
                                tlp.BackgroundImage = Resources.SquareUp;
                            }
                            break;
                        case 2:
                            if (j==2)
                            {
                                tlp.BackgroundImage = Resources.SquareFinal;
                            }
                            else if (j==4)
                            {
                                tlp.BackgroundImage = Resources.SquareLeft;
                            }
                            break;
                        default:
                            break;
                    }
                    panel.Controls.Add(tlp);
        
                    panels[GetIndex(i, j)] = panel;
                    tlpTablero.Controls.Add(panel, j, i);
                    tlp.Size = panel.Size;
                }
            }
            SetPlayersAtStart();
        }

        private void SetPlayersAtStart()
        {
            foreach (var player in board.Players)
            {
                var picBox = new PictureBox();

                switch (player.Index)
                {
                    case 0:
                        picBox.Image = Resources.Player;
                        break;
                    case 1:
                        picBox.Image = Resources.Player2;
                        break;
                    case 2:
                        picBox.Image = Resources.Player3;
                        break;
                    case 3:
                        picBox.Image = Resources.Player4;
                        break;
                    default:
                        break;
                }

                picBoxes[player.Index] = picBox;
                var tlp = (TableLayoutPanel)panels[player.Location.Index].Controls[0];

                picBox.Anchor = tlp.Anchor;

                picBox.Size = new Size(tlp.Width / 2, tlp.Size.Height / 2);
                picBox.Location = new Point(0, 0);

                tlp.Controls.Add(picBox, player.Index % 2, player.Index / 2);
            }
        }

        private int GetIndex(int i, int j)
        {
            switch (i)
            {
                case 0:
                    return 18 - j;
                case 1:
                    switch (j)
                    {
                        case 0:
                            return 18;
                        case 6:
                            return 10;
                        default:
                            return 34 - j;
                    }
                case 2:
                    switch (j)
                    {
                        case 0:
                            return 19;
                        case 1:
                            return 34;
                        case 5:
                            return 28;
                        case 6:
                            return 9;
                        default:
                            return 43 - j;
                    }
                case 3:
                    switch (j)
                    {
                        case 0:
                            return 20;
                        case 5:
                            return 27;
                        case 6:
                            return 8;
                        default:
                            return 34 + j;
                    }
                case 4:
                    switch (j)
                    {
                        case 6:
                            return 7;
                        default:
                            return 21 + j;
                    }
                default:
                    return j;
            }
        }

    }
}
